package jsonparser;

import jsonparser.core.parser.Builder;
import jsonparser.core.parser.JSONParser;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;

/**
 * Created by pendragon on 17-5-26.
 */
public class Main {

    private static Builder parse(File jsonFile){
        JSONParser parser = new JSONParser(jsonFile);
        try {
            return parser.parse();
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    private static void pretty(File inFile, File outFile){
        Builder b = parse(inFile);
        if (b != null){
            String s = b.build().toString();
            try {
                Files.write(outFile.toPath(), s.getBytes(), StandardOpenOption.CREATE_NEW);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static void search(File file, String searchPath){
        Builder b = parse(file);
        if (b != null){
            String[] paths = searchPath.split("/");
            Object result = find(b.build(), paths, 1);
            System.out.println(result);
        }
    }

    private static Object find(Object upObject, String[] paths, int i){
        if (upObject instanceof JSONObject || upObject instanceof JSONArray){
            int tem = paths[i].indexOf('[');

            try {
                if (tem == -1){
                    Object o =  findInObject(upObject, paths[i]);
                    return find(o, paths, i + 1);
                } else {
                    int index = Integer.parseInt(paths[i].substring(tem + 1, paths[i].length() - 1));
                    Object o = findInArray(findInObject(upObject, paths[i].substring(0, tem)), index);
                    return find(o, paths, i + 1);
                }
            } catch (ArrayIndexOutOfBoundsException e){
                System.out.println("illegal search path");
                System.exit(1);
            }

        }

        // 一切正常的话, 应该能在这里找到
        return upObject;
    }

    private static Object findInArray(Object o, int index){
        if (o instanceof JSONArray){
            try {
                return ((JSONArray)o).get(index);
            } catch (IndexOutOfBoundsException e){
                System.out.println("null");
                System.exit(0);
            }
        }

        return null;
    }

    private static Object findInObject(Object o, String key){
        if (o instanceof JSONObject)
            return ((JSONObject)o).getObject(key);

        return null;
    }

    public static void main(String[] args) {
        if (args.length == 1){
            File file = new File(args[0]);
            parse(file);
        } else if (args.length == 2){
            if (!args[0].equals("-pretty"))
                return;

            String inFilePath = args[1];
            File inFile = new File(inFilePath);
            String newFileName = inFile.getName().substring(0, inFile.getName().indexOf('.'));
            File outFile = new File(inFile.getParent() + File.separator + newFileName + ".pretty." + "json");
            pretty(inFile, outFile);
        } else if (args.length == 3){
            if (!args[0].equals("-search"))
                return;

            File file = new File(args[1]);
            search(file, args[2]);
        } else {
            help();
        }
    }

    private static void help(){
        System.out.println("Usage: ");
        System.out.println("1. java -jar jsonparser.jar [-pretty] *.json");
        System.out.println("like, java -jar jsonparser.jar -pretty test/e/country.json");
        System.out.println("2. java -jar jsonparser.jar -search *.json $searchpath");
        System.out.println("like, java -jar jsonparser.jar -search test/f/country.json /RECORDS[35]/countryname");
    }
}
